﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Xml.Linq;
using System.Web.Mvc;
using System.Web.Routing;
using LevenBlog.Core.Utils;
using LevenBlog.Core.Configuretion.Route;

namespace LevenBlog.Web
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            ViewEngines.Engines.Clear();
            InitUrlRouting(RouteTable.Routes);
        }

        protected static void InitUrlRouting(RouteCollection routes)
        {
            RouteConfigurationSection section =
                (RouteConfigurationSection)ConfigurationManager.GetSection("routeConfiguration");

            if (null == section) {
                throw new Exception("还没有配置你的Route规则,请配置你的Route规则");
            }

            RouteTable.Routes.RegisterRoutes(section);
            ////加入blog短route
            //routes.MapRoute("shortbloglist", string.Format("list{0}/{{category}}/{{page}}", WebConfig.ExtendName), new { controller = "blog", action = "list", category = "0", page = "1" }, new { category = "\\d+", page = "\\d+" });
            //routes.MapRoute("shortblogdefaultlist", string.Format("list{0}/{{page}}", WebConfig.ExtendName), new { controller = "blog", action = "list", category = "0", page = "1" }, new { page = "\\d+" });
            //routes.MapRoute("shortblogindex", string.Format("index{0}/{{page}}", WebConfig.ExtendName), new { controller = "blog", action = "index", page = "1" }, new { page = "\\d+" });
            //routes.MapRoute("shortblogview", string.Format("view{0}/{{id}}/{{page}}", WebConfig.ExtendName), new { controller = "blog", action = "view", id = "0", page = "1" }, new { id = "\\d+", page = "\\d+" });
            //routes.MapRoute("shortblogrss", string.Format("rss/{{category}}{0}/{{page}}", WebConfig.ExtendName), new { controller = "blog", action = "rss", category = "0", page = "1" }, new { category = "\\d+", page = "\\d+" });
            //routes.MapRoute("shortblogatom", string.Format("atom{0}/{{category}}/{{page}}", WebConfig.ExtendName), new { controller = "blog", action = "atom", category = "0", page = "1" }, new { category = "\\d+", page = "\\d+" });
            //routes.MapRoute("shortblogarchive", string.Format("archive{0}/{{year}}/{{month}}/{{day}}/{{page}}", WebConfig.ExtendName), new { controller = "blog", action = "archive", year = "0", month = "0", day = "0", page = "1" }, new { year = "\\d+", month = "\\d+", day = "\\d+", page = "\\d+" });
            //routes.MapRoute("shortcommentview", string.Format("comment{0}/{{page}}", WebConfig.ExtendName), new { controller = "blog", action = "comment", page = "1" }, new { page = "\\d+" });
            //routes.MapRoute("shorttagview", string.Format("tag{0}/{{name}}/{{page}}", WebConfig.ExtendName), new { controller = "blog", action = "tag", name = "", page = "1" }, new { page = "\\d+" });
            //routes.MapRoute("shortblogsearch", string.Format("search{0}/{{searchstring}}/{{page}}", WebConfig.ExtendName), new { controller = "blog", action = "search", searchstring = "", page = "1" }, new { page = "\\d+" });
            //routes.MapRoute("shortblogdowithid", string.Format("blog{0}/{{action}}/{{id}}", WebConfig.ExtendName), new { controller = "blog", action = "index", id = "0" }, new { id = "\\d+" });
            //routes.MapRoute("shortblogdefault", string.Format("blog{0}/{{action}}", WebConfig.ExtendName), new { controller = "blog", action = "index" });
     
            //blog部分标准route
            //routes.MapRoute("bloglist", string.Format("blog{0}/list/{{category}}/{{page}}", WebConfig.ExtendName), new { controller = "blog", action = "list", category = "0", page = "1" }, new { category = "\\d+", page = "\\d+" });
            //routes.MapRoute("blogdefaultlist", string.Format("blog{0}/list/{{page}}", WebConfig.ExtendName), new { controller = "blog", action = "list", category = "0", page = "1" }, new { page = "\\d+" });
            //routes.MapRoute("blogindex", string.Format("blog{0}/index/{{page}}", WebConfig.ExtendName), new { controller = "blog", action = "index", page = "1" }, new { page = "\\d+" });
            //routes.MapRoute("blogview", string.Format("blog{0}/view/{{id}}/{{page}}", WebConfig.ExtendName), new { controller = "blog", action = "view", id = "0", page = "1" }, new { id = "\\d+", page = "\\d+" });
            //routes.MapRoute("blogrss", string.Format("blog{0}/rss/{{category}}/{{page}}", WebConfig.ExtendName), new { controller = "blog", action = "rss", category = "0", page = "1" }, new { category = "\\d+", page = "\\d+" });
            //routes.MapRoute("blogatom", string.Format("blog{0}/atom/{{category}}/{{page}}", WebConfig.ExtendName), new { controller = "blog", action = "atom", category = "0", page = "1" }, new { category = "\\d+", page = "\\d+" });           
            //routes.MapRoute("blogarchive", string.Format("blog{0}/archive/{{year}}/{{month}}/{{day}}/{{page}}", WebConfig.ExtendName), new { controller = "blog", action = "archive", year = "0", month = "0", day = "0", page = "1" }, new { year = "\\d+", month = "\\d+", day = "\\d+", page = "\\d+" });
            //routes.MapRoute("commentview", string.Format("blog{0}/comment/{{page}}", WebConfig.ExtendName), new { controller = "blog", action = "comment", page = "1" }, new { page = "\\d+" });
            //routes.MapRoute("tagview", string.Format("blog{0}/tag/{{name}}/{{page}}", WebConfig.ExtendName), new { controller = "blog", action = "tag", name = "", page = "1" }, new { page = "\\d+" });
            //routes.MapRoute("blogsearch", string.Format("blog{0}/search/{{searchstring}}/{{page}}", WebConfig.ExtendName), new { controller = "blog", action = "search", searchstring = "", page = "1" }, new { page = "\\d+" });
            ////routes.MapRoute("blogdowithpage", string.Format("blog{0}/{{action}}/{{page}}", WebConfig.ExtendName), new { controller = "blog", action = "index", page = "0" }, new { page = "\\d+" });
            //routes.MapRoute("blogdowithid", string.Format("blog{0}/{{action}}/{{id}}", WebConfig.ExtendName), new { controller = "blog", action = "index", id = 0 }, new { id = "\\d*" });
            //routes.MapRoute("blogdefault", string.Format("blog{0}/{{action}}", WebConfig.ExtendName), new { controller = "blog", action = "index" });
            ////user部分
            //routes.MapRoute("userview", string.Format("user{0}/view/{{name}}", WebConfig.ExtendName), new { controller = "user", action = "view", name = "" });
            //routes.MapRoute("userreg", string.Format("user{0}/reg/{{step}}", WebConfig.ExtendName), new { controller = "user", action = "reg", step = "1" }, new { step = "[1|2]" });
            //routes.MapRoute("useraction", string.Format("user{0}/{{action}}", WebConfig.ExtendName), new { controller = "user", action = "login" });
            ////admin部分
            //routes.MapRoute("adminlist", string.Format("admin{0}/{{action}}/{{page}}/{{pagesize}}", WebConfig.ExtendName), new { controller = "admin", action = "noaction", page = "1", pagesize = "20" }, new { page = "\\d+", pagesize = "\\d+" });
            //routes.MapRoute("admincommentdelete", string.Format("admin{0}/commentdelete/{{idarray}}", WebConfig.ExtendName), new { controller = "admin", action = "commentdelete", idarray = "" }, new { idarray = "\\d+\\d,*" });
            //routes.MapRoute("adminskinchange", string.Format("admin{0}/skinchange/{{path}}", WebConfig.ExtendName), new { controller = "admin", action = "skinchange", path = "" });
            //routes.MapRoute("admindoaction", string.Format("admin{0}/{{action}}/{{id}}", WebConfig.ExtendName), new { controller = "admin", action = "noaction", id = "0" }, new { id = "\\d+" });
            //routes.MapRoute("adminaction", string.Format("admin{0}/{{action}}", WebConfig.ExtendName), new { controller = "admin", action = "noaction" });
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}